# 共用的自定义工具类
from flask import current_app
from flask import g
from flask import session
import functools
from info.models import User


def do_index_class(index):
    if index == 0:
        return "first"
    elif index == 1:
        return "second"
    elif index == 2:
        return "third"
    else:
        return ""

def user_login_data(f):
    # 使用functools.wraps装饰器是为了防止同一个装饰器能将不同名函数改成同一个函数名,从而防止程序报错的作用
    @functools.wraps(f)
    def wrapper(*args, **kwargs):
        user_id = session.get("user_id")
        user = None
        if user_id:
            try:
                user = User.query.get(user_id)
            except Exception as e:
                current_app.logger.error(e)
        g.user = user
        return f(*args, **kwargs)
    return wrapper
